﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using System.Globalization;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO: 1、可以选择当前时间的前后天数
    ///       2、支持更多的时间格式，如月-日
    /// </summary>
    public class DateTime2TextNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            DateTime2TextNParam? param = item.ControlParam as DateTime2TextNParam;
            string outVarName = param.OutVarName;
            this.GetStringVar(ref outVarName, globleData);

            string dateobj = param.DateObj;
            DateTime operateTarget = this.GetObjVar<DateModel>(ref dateobj, globleData).DateValue;

            int formatType = param.FormatType;
            string separator = param.Separator;
            this.GetStringVar(ref separator, globleData);

            string fromatString;
            if(separator == null) { 
                separator = string.Empty;
            }
            switch(formatType) {
                case 0:
                    fromatString = operateTarget.ToString($"yyyy{separator}MM{separator}dd", DateTimeFormatInfo.InvariantInfo);
                    break;
                case 1:
                    fromatString = operateTarget.ToString($"yyyy{separator}MM", DateTimeFormatInfo.InvariantInfo);
                    break;
                case 2:
                    fromatString = operateTarget.ToString($"M{separator}dd", DateTimeFormatInfo.InvariantInfo);
                    break;
                case 3:
                    fromatString = operateTarget.ToString($"yyyy", DateTimeFormatInfo.InvariantInfo);
                    break;
                case 4:
                    fromatString = operateTarget.Month.ToString();
                    break;
                case 5:
                    fromatString = operateTarget.Day.ToString();
                    break;
                default:
                    fromatString = operateTarget.ToString($"yyyy{separator}MM{separator}dd", DateTimeFormatInfo.InvariantInfo);
                    break;
            }
            this.saveVar(globleData, outVarName, new StringModel(outVarName, fromatString), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
